<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Lib\Params\AjaxReturnParams;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Input;
use function var_dump;

class UploadController extends Controller {

    public function ajaxFileUpload() {
        echo $this->_getFileUrl('file');
    }

    public function ajaxImageUpload() {
        echo $this->_getFileUrl('image');
    }

    private function _getFileUrl($fileName)
    {
        $allowExt = array('jpg','jpeg','png','gif','bmp','txt','doc','xls','zip');
        $fileUrl = $this->_uploadFile($fileName,$allowExt);
        $ajaxResult = new AjaxReturnParams();
        if(-1 === $fileUrl){
            $ajaxResult->code=-1;
            $ajaxResult->msg='上传文件为空！';
            return json_encode($ajaxResult);
        }
        if(-2 === $fileUrl){
            $ajaxResult->code=-2;
            $ajaxResult->msg='上传的文件格式错误，只能是'.implode(',',$allowExt).'格式！';
            return json_encode($ajaxResult);
        }
        $ajaxResult->data = $fileUrl;
        return json_encode($ajaxResult);
    }

    public function ajaxCKEditorUpload() {
        $url = $this->_uploadFile('upload');
        $callback = $_REQUEST["CKEditorFuncNum"];
        echo "<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction($callback,'".$url."','');</script>";
    }


    private function _uploadFile($inputName,$allowExt = array('jpg','jepg','png','gif','bmp','txt','doc','xls','zip'))
    {
        $file = Input::file($inputName);
        if(is_null($file) || empty($file)){
            return -1;
        }
        $ext = $file->guessClientExtension();

        if(!in_array($ext,$allowExt)){
            return -2;
        }
        $uploadDomainPath = Config::get('filesystems.upload.uploadDomainPath', '/data/www/upload/sanangude/');
        $path = date('Ymd').DIRECTORY_SEPARATOR;
        $destinationPath = $uploadDomainPath.$path;
        $filename = md5(date('YmdHis').$file->getClientOriginalName()).rand().'.'.$ext;
        $file->move($destinationPath, $filename);
        $imageDomain = Config::get('filesystems.upload.imageDomain', 'http://img.joycate.com/sanangude/');
        return $imageDomain.$path.$filename;
    }

    /**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		//
	}

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}

}
